package com.sanbi.erp.db.util;

import com.github.houbb.property.bs.PropertyBs;
import com.sanbi.erp.core.config.UserConfig;
import com.sanbi.erp.core.system.Enums;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * @introduce: 积分账单工具包
 * @Date: 2022/8/7 16:04
 * @Author: XYP
 */
public class ScoreBillUtil {
    // 购买成功 / 退款成功
    public static final Integer BUY_OK = 0;
    public static final Integer REFUND_OK = 1;
    public static final Integer SALES_RETURN_OK = 2;
    public static final String BUY;
    public static final String REFUND;
    public static final String SALES_RETURN;
    public static final String SCORE_BILL_DESC;
    // 用户积分对应的会员等级
    public static final Byte STAR_ONE_SCORE = 1;
    public static final Byte STAR_TWO_SCORE = 2;
    public static final Byte STAR_THREE_SCORE = 3;
    public static final Byte STAR_FOUR_SCORE = 4;
    public static final Byte STAR_FIVE_SCORE = 5;
    // 折扣
    public static final Integer DISCOUNT = 100;

    // 初始化变量
    static {
        UserConfig userConfig = new UserConfig();
        PropertyBs.getInstance("user.properties").asBean(userConfig);
        BUY = userConfig.getBuy();
        REFUND = userConfig.getRefunds();
        SALES_RETURN = userConfig.getSalesReturn();
        SCORE_BILL_DESC = userConfig.getScoreBillDesc();
    }

    /**
     * 根据用户积分 计算用户的会员等级
     *
     * @param totalScores 用户积分
     * @return
     */
    public static Byte userLevel(BigDecimal totalScores) {
        // 获取会员配置信息
        UserConfig userConfig = new UserConfig();
        PropertyBs.getInstance("user.properties").asBean(userConfig);
        // 1-5级 对应的积分
        Integer star1Score = userConfig.getStar1Score();
        BigDecimal bigDecimal1 = BigDecimal.valueOf(star1Score);
        Integer star2Score = userConfig.getStar2Score();
        BigDecimal bigDecimal2 = BigDecimal.valueOf(star2Score);
        Integer star3Score = userConfig.getStar3Score();
        BigDecimal bigDecimal3 = BigDecimal.valueOf(star3Score);
        Integer star4Score = userConfig.getStar4Score();
        BigDecimal bigDecimal4 = BigDecimal.valueOf(star4Score);

        // 会员等级
        Byte userLevel = 1;
        // if (totalScores < 0) userLevel = STAR_ONE_SCORE;
        if (totalScores.compareTo(new BigDecimal("0.00")) >= 0 && totalScores.compareTo(bigDecimal1) < 0)
            userLevel = STAR_ONE_SCORE;
        if (totalScores.compareTo(bigDecimal1) >= 0 && totalScores.compareTo(bigDecimal2) < 0)
            userLevel = STAR_TWO_SCORE;
        if (totalScores.compareTo(bigDecimal2) >= 0 && totalScores.compareTo(bigDecimal3) < 0)
            userLevel = STAR_THREE_SCORE;
        if (totalScores.compareTo(bigDecimal3) >= 0 && totalScores.compareTo(bigDecimal4) < 0)
            userLevel = STAR_FOUR_SCORE;
        if (totalScores.compareTo(bigDecimal4) >= 0) userLevel = STAR_FIVE_SCORE;

        return userLevel;
    }

    /**
     * 根据用户等级获取对应的积分
     *
     * @param userLevel 用户等级
     * @return
     */
    public static Integer getIntegral(Byte userLevel) {
        // 获取会员配置信息
        UserConfig userConfig = new UserConfig();
        PropertyBs.getInstance("user.properties").asBean(userConfig);
        // 1-5级 对应的积分
        Integer star1Score = userConfig.getStar1Score();
        Integer star2Score = userConfig.getStar2Score();
        Integer star3Score = userConfig.getStar3Score();
        Integer star4Score = userConfig.getStar4Score();
        Integer star5Score = userConfig.getStar5Score();

        Integer score = 0;
        if (userLevel.equals(STAR_ONE_SCORE)) score = star1Score;
        if (userLevel.equals(STAR_TWO_SCORE)) score = star2Score;
        if (userLevel.equals(STAR_THREE_SCORE)) score = star3Score;
        if (userLevel.equals(STAR_FOUR_SCORE)) score = star4Score;
        if (userLevel.equals(STAR_FIVE_SCORE)) score = star5Score;

        return score;
    }

    /**
     * 根据用户等级获取对应的分销奖金比例
     *
     * @param userLevel 用户等级
     * @return
     */
    public static Integer getStarBroker(Byte userLevel) {
        // 获取会员配置信息
        UserConfig userConfig = new UserConfig();
        PropertyBs.getInstance("user.properties").asBean(userConfig);
        // 1-5级 对应的分销奖金比例
        Integer star1Broker = userConfig.getStar1Broker();
        Integer star2Broker = userConfig.getStar2Broker();
        Integer star3Broker = userConfig.getStar3Broker();
        Integer star4Broker = userConfig.getStar4Broker();
        Integer star5Broker = userConfig.getStar5Broker();

        Integer starBroker = 0;
        if (userLevel.equals(STAR_ONE_SCORE)) {
            starBroker = star1Broker;
        }
        if (userLevel.equals(STAR_TWO_SCORE)) {
            starBroker = star2Broker;
        }
        if (userLevel.equals(STAR_THREE_SCORE)) {
            starBroker = star3Broker;
        }
        if (userLevel.equals(STAR_FOUR_SCORE)) {
            starBroker = star4Broker;
        }
        if (userLevel.equals(STAR_FIVE_SCORE)) {
            starBroker = star5Broker;
        }

        return starBroker;
    }

    /**
     * 获取用户折扣描述和会员描述
     *
     * @param userLevel 用户等级
     * @param score     用户积分
     * @return
     */
    public static Object getDiscount(Byte userLevel, BigDecimal score) {
        // 获取会员配置信息
        UserConfig userConfig = new UserConfig();
        PropertyBs.getInstance("user.properties").asBean(userConfig);
        // 1-5级 对应的积分
        Integer star1Score = userConfig.getStar1Score();
        BigDecimal bigDecimal1 = BigDecimal.valueOf(star1Score);
        Integer star2Score = userConfig.getStar2Score();
        BigDecimal bigDecimal2 = BigDecimal.valueOf(star2Score);
        Integer star3Score = userConfig.getStar3Score();
        BigDecimal bigDecimal3 = BigDecimal.valueOf(star3Score);
        Integer star4Score = userConfig.getStar4Score();
        BigDecimal bigDecimal4 = BigDecimal.valueOf(star4Score);

//        User user = new User();
        // 升级所需经验值 列如：需要继续购物获得 【4000】 经验值可升级
        BigDecimal buyDescribed = new BigDecimal("0.00");

        Enums.UserLevel item = Enums.UserLevel.getItem(userLevel);
//        user.setUserLevelName(item.getName());// 会员名称
//        user.setDiscountDesc(item.getDiscountDesc());// 会员

        if (userLevel.equals(STAR_ONE_SCORE)) {
            buyDescribed = bigDecimal1.subtract(score);
        }
        if (userLevel.equals(STAR_TWO_SCORE)) {
            buyDescribed = bigDecimal2.subtract(score);
        }
        if (userLevel.equals(STAR_THREE_SCORE)) {
            buyDescribed = bigDecimal3.subtract(score);
        }
        if (userLevel.equals(STAR_FOUR_SCORE)) {
            buyDescribed = bigDecimal4.subtract(score);
        }
        if (userLevel.equals(STAR_FIVE_SCORE)) {
            buyDescribed = score;
        }


        Map<Object, Object> map = new HashMap<>();
//        map.put("user", user);
        map.put("buyDescribed", buyDescribed);

        return map;
    }

}
